﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Hallenberg.SmartClient.Interfaces;
using Hallenberg.SmartClient.TypeHandlers;
using Hallenberg.SmartClient.Services;
using NUnit.Framework;

namespace Hallenberg.SmartClient.Test.TypeHandlers
{
    [TestFixture]
    public class BaseModuleHandlerTest
    {
        #region Embedded helper class

        public class MyTestModule : BaseModule
        {
            public delegate void OnEvent();
            public static OnEvent ExecutedFirst;
            public static OnEvent ExecutedSecond;
            public static OnEvent ExecutedThird;

            public override void RegisterServices(IServiceLocator services)
            {
                if (services == null)
                {
                    throw new ArgumentNullException("services");
                }

                if (MyTestModule.ExecutedFirst != null)
                {
                    MyTestModule.ExecutedFirst();
                }
            }

            public override void RegisterTranslators(IEntityTranslatorService entityTranslatorService)
            {
                if (entityTranslatorService == null)
                {
                    throw new ArgumentNullException("entityTranslatorService");
                }

                if (MyTestModule.ExecutedSecond != null)
                {
                    MyTestModule.ExecutedSecond();
                }
            }

            public override void RegisterEvents(IEventManager events)
            {
                if (events == null)
                {
                    throw new ArgumentNullException("events");
                }

                if (MyTestModule.ExecutedThird != null)
                {
                    MyTestModule.ExecutedThird();
                }
            }
        }

        #endregion

        private WorkItemController m_WorkItem;

        [SetUp]
        public void Initialize()
        {
            m_WorkItem = new WorkItemController();
            m_WorkItem.Services.RegisterType<IClassTypeHandler, BaseModuleHandler>();
        }

        [Test]
        public void SmartClient_TypeHandlers_BaseModuleHandler_Handle()
        {
            IClassTypeHandler handler = m_WorkItem.Services.Resolve<IClassTypeHandler>();
            Assert.IsTrue(handler.CanHandle(typeof(MyTestModule)));

            bool executedFirst = false;
            bool executedSecond = false;
            bool executedThird = false;

            MyTestModule.ExecutedFirst = new MyTestModule.OnEvent(() => { executedFirst = true; });
            MyTestModule.ExecutedSecond = new MyTestModule.OnEvent(() => { executedSecond = true; });
            MyTestModule.ExecutedThird = new MyTestModule.OnEvent(() => { executedThird = true; });

            Assert.IsTrue(handler.Handle(typeof(MyTestModule)));

            m_WorkItem.Initialize();

            Assert.That(executedFirst, Is.True);
            Assert.That(executedSecond, Is.True);
            Assert.That(executedThird, Is.True);
        }
    }
}
